<template>
  <el-container style="padding: 15px">
    <el-header height="120px">
      <el-form size="small" label-width="70px">
        <el-row type="flex" :gutter="20" align="middle">
          <el-col :span="6">
            <el-form-item label="项目编号">
              <el-input v-model="searchObj.reportId" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目名称">
              <el-input v-model="searchObj.reportName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="业务人员">
              <el-select v-model="searchObj.salesman" style="width: 100%" clearable filterable>
                <el-option v-for="item in users" :key="item.id" :value="item.id" :label="item.nickName" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="负责人员">
              <el-select v-model="searchObj.principal" style="width: 100%" clearable filterable>
                <el-option v-for="item in users" :key="item.id" :value="item.id" :label="item.nickName" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" align="middle" :gutter="20">
          <el-col :span="6">
            <el-form-item label="是否收费">
              <el-select v-model="searchObj.charge" style="width: 100%" clearable filterable>
                <el-option :value="true" :label="'是'" />
                <el-option :value="false" :label="'否'" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="票据情况">
              <el-select v-model="searchObj.billDetails" style="width: 100%" clearable filterable>
                <el-option :value="1" :label="'发票'" />
                <el-option :value="2" :label="'收据'" />
                <el-option :value="3" :label="'无票'" />
                <el-option :value="4" :label="'未填写'" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="到账日期">
              <el-date-picker
                v-model="searchObj.startTime"
                value-format="timestamp"
                type="date"
                placeholder="开始时间"
                @change="startTimeChangeHandler"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label=" ">
              <el-date-picker
                v-model="searchObj.endTime"
                value-format="timestamp"
                type="date"
                placeholder="结束时间"
                @change="EndTimeChangeHandler"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" align="center" justify="center">
          <el-col :span="4">
            <el-button type="primary" size="small" @click="getProjectList">搜索</el-button>
            <el-button size="small" @click="clearSearch">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-divider />
    <el-main>
      <el-table :data="projectList">
        <el-table-column label="项目编号" prop="reportId" width="170px">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="toDetails(scope.row.id)">{{ scope.row.reportId }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" prop="reportName" width="300px" :formatter="subName" />
        <el-table-column label="负责人员" prop="principal" />
        <el-table-column label="业务人员" prop="salesman" />
        <el-table-column label="实际到账" prop="financeArrivalAmount" />
        <el-table-column label="到账时间" prop="financePayDate" :formatter="tableTimeFormat" />
        <el-table-column label="开票金额" prop="financeBillCharge" />
        <el-table-column label="票据情况" prop="financeBillStatus" align="bill">
          <template scope="bill">
            <p v-if="bill.row.financeBillStatus == '0'">未填写</p>
            <p v-if="bill.row.financeBillStatus == '1'">发票</p>
            <p v-if="bill.row.financeBillStatus == '2'">收据</p>
            <p v-if="bill.row.financeBillStatus == '3'">无票</p>
          </template>
        </el-table-column>
        <el-table-column label="开票时间" prop="financeBillDate" :formatter="tableTimeFormat" />
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-main>
  </el-container>
</template>

<script>
import { searchFinanceProject, getSearchFormList } from '@/api/project-report'
import { ellipsisString, parseTime } from '@/utils'

export default {
  name: 'Index',
  data() {
    return {
      searchObj: {},
      projectType: [],
      achievement: [],
      leaf: [],
      users: [],
      projectList: [],
      currentPage: 1,
      sizePage: 10,
      totalPage: 0
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.getProjectList()
      this.getSearchForm()
    },

    getProjectList() {
      const currentPage = this.currentPage
      const sizePage = this.sizePage
      const searchObj = this.searchObj
      searchFinanceProject(searchObj, currentPage, sizePage).then(res => {
        this.projectList = res.data.list
        this.totalPage = res.data.total
      })
    },
    clearSearch() {
      this.searchObj = {}
      this.getProjectList()
      // 重置成功提示框
      this.$message({
        message: '重置成功',
        type: 'success'
      })
    },
    subName(row, column, cellValue, index) {
      return ellipsisString(cellValue)
    },
    getSearchForm() {
      getSearchFormList().then(res => {
        this.projectType = res.data.projectType
        this.achievement = res.data.achievement
        this.leaf = res.data.leaf
        this.users = res.data.users
      })
    },
    handleSizeChange(val) {
      this.sizePage = val
      this.getProjectList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getProjectList()
    },
    toDetails(id) {
      this.$router.push({ path: '/report/detailsProject', query: { id: id }})
    },
    tableTimeFormat(row, column, cellValue, index) {
      return parseTime(cellValue, '')
    }
  }
}
</script>

<style scoped>

</style>
